package com.happy_hao.topbiz.controller;

import com.happy_hao.topbiz.api.UserApi;
import com.happy_hao.topbiz.common.Result;
import com.happy_hao.topbiz.dto.userDto.UpdateUserRequest;
import com.happy_hao.topbiz.exception.ServiceException;
import com.happy_hao.topbiz.po.User;
import com.happy_hao.topbiz.service.EmailCodeService;
import com.happy_hao.topbiz.service.RegisterService;
import com.happy_hao.topbiz.token.EmailCodeToken;
import com.happy_hao.topbiz.dto.EmailCodeRequest;
import com.happy_hao.topbiz.dto.FormRequest;
import com.happy_hao.topbiz.service.Oauth2Service;
import com.happy_hao.topbiz.token.Oauth2Token;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/topbiz/anon")
public class AnonController {

    @Resource
    private Oauth2Service oauth2Service;

    @Resource
    private EmailCodeService emailCodeService;

    @Resource
    private RegisterService registerService;

    @Resource
    private UserApi userApi;

    @PostMapping("/login/form")
    public Result formLogin(@Valid @RequestBody FormRequest formRequest) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(formRequest.getIdentifier(), formRequest.getPassword());
        subject.login(token);
        return Result.success().message("登录成功");
    }

    @PostMapping("/register/form")
    public Result formRegister(@Valid @RequestBody FormRequest formRequest) {
        return registerService.form(formRequest);
    }

    @PostMapping("/login/email-code")
    public Result emailCodeLogin(@Valid @RequestBody EmailCodeRequest emailCodeRequest) {
        Subject subject = SecurityUtils.getSubject();
        EmailCodeToken token = new EmailCodeToken(emailCodeRequest.getEmail(), emailCodeRequest.getCode());
        subject.login(token);
        return Result.success().message("登录成功");
    }

    @PostMapping("/register/email-code")
    public Result emailCodeRegister(@Valid @RequestBody EmailCodeRequest emailCodeRequest) {
        return registerService.code(emailCodeRequest);
    }

    @GetMapping("/login/feishu")
    public Result feishu(@RequestParam String code) {
        Subject subject = SecurityUtils.getSubject();
        String phone = oauth2Service.getPhoneByCode(code, true);
        Oauth2Token token = new Oauth2Token(phone, "feishu");
        subject.login(token);
        return Result.success().message("登录成功");
    }

    @GetMapping("/register/feishu")
    public Result feishuRegister(@RequestParam String code) {
        return registerService.feishu(code);
    }

    @GetMapping("/getEmailCode")
    public Result getEmailCode(@RequestParam @Email String email) {
        return emailCodeService.getEmailCode(email);
    }

    @GetMapping("/logout")
    public Result logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null && subject.isAuthenticated()) {
            // 执行登出操作
            subject.logout();
        }
        //重定向到登录页面
        return Result.success().message("登出成功");
    }

    @PostMapping("/forgetPassword/email-code")
    public Result forgetPassword(@RequestParam @Pattern(regexp = "^(?=.*[0-9])(?=.*[a-zA-Z])[a-zA-Z0-9]{8,20}$", message = "密码必须是8-20位，包含字母和数字") String password, @Valid @RequestBody EmailCodeRequest emailCodeRequest) {
        Subject subject = SecurityUtils.getSubject();
        EmailCodeToken token = new EmailCodeToken(emailCodeRequest.getEmail(), emailCodeRequest.getCode());
        subject.login(token);
        UpdateUserRequest updateUserRequest = new UpdateUserRequest();
        DefaultPasswordService passwordService = new DefaultPasswordService();
        updateUserRequest.setEncryptedPassword(passwordService.encryptPassword(password));
        userApi.updateUser(updateUserRequest);
        return Result.success().message("已修改为新密码");
    }
}
